Re: [GENERAL] The value returned by autoinc ? - Mailing list pgsql-general

From Herouth Maoz
Subject Re: [GENERAL] The value returned by autoinc ?
Date
Msg-id l03110702b31288079b29@[147.233.159.109]
Whole thread Raw
In response to The value returned by autoinc ?  (Silvio Emanuel Barbosa de Macedo <smacedo@inescn.pt>)
Responses Re: [GENERAL] The value returned by autoinc ?
List pgsql-general
At 2:57 +0200 on 15/3/99, Silvio Emanuel Barbosa de Macedo wrote:


> When I insert data into a table with a sequence associated to a column
> (and the required trigger), how can I know the value the sequence has
> just generated ? (think in parallel accesses)
>
> Would this approach be the answer ?
> begin work
>     insert...
>     select max...
> commit

No, this approach is a waste of precious time... The correct approach is:

   INSERT...
   SELECT currval( 'seq_name' );

currval gives you the last value the sequence has given to the current
session. That is, it won't work if you use it before the insertion (because
the sequence didn't give you a number yet). It will also give you the
correct number even if between the INSERT and the SELECT, another process
or another connection also made an insert.

Herouth

--
Herouth Maoz, Internet developer.
Open University of Israel - Telem project
http://telem.openu.ac.il/~herutma



pgsql-general by date:

Previous
From: "Bernhard Krick"
Date:
Subject: how to start postmaster at system boot on linux?
Next
From: Silvio Emanuel Barbosa de Macedo
Date:
Subject: Re: [GENERAL] The value returned by autoinc ?